<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - include/caffe/layer.hpp</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">include/caffe</a> - layer.hpp<span style="font-size: 80%;"> (source / <a href="layer.hpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">67</td>
            <td class="headerCovTableEntry">112</td>
            <td class="headerCovTableEntryLo">59.8 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">17</td>
            <td class="headerCovTableEntry">56</td>
            <td class="headerCovTableEntryLo">30.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #ifndef CAFFE_LAYER_H_</a>
<span class="lineNum">       2 </span>            : #define CAFFE_LAYER_H_
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &lt;algorithm&gt;
<span class="lineNum">       5 </span>            : #include &lt;string&gt;
<span class="lineNum">       6 </span>            : #include &lt;vector&gt;
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &quot;caffe/blob.hpp&quot;
<span class="lineNum">       9 </span>            : #include &quot;caffe/common.hpp&quot;
<span class="lineNum">      10 </span>            : #include &quot;caffe/layer_factory.hpp&quot;
<span class="lineNum">      11 </span>            : #include &quot;caffe/proto/caffe.pb.h&quot;
<span class="lineNum">      12 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">      13 </span>            : 
<span class="lineNum">      14 </span>            : /**
<span class="lineNum">      15 </span>            :  Forward declare boost::thread instead of including boost/thread.hpp
<span class="lineNum">      16 </span>            :  to avoid a boost/NVCC issues (#1009, #1010) on OSX.
<span class="lineNum">      17 </span>            :  */
<span class="lineNum">      18 </span>            : namespace boost { class mutex; }
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : namespace caffe {
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : /**
<span class="lineNum">      23 </span>            :  * @brief An interface for the units of computation which can be composed into a
<span class="lineNum">      24 </span>            :  *        Net.
<span class="lineNum">      25 </span>            :  *
<span class="lineNum">      26 </span>            :  * Layer%s must implement a Forward function, in which they take their input
<span class="lineNum">      27 </span>            :  * (bottom) Blob%s (if any) and compute their output Blob%s (if any).
<span class="lineNum">      28 </span>            :  * They may also implement a Backward function, in which they compute the error
<span class="lineNum">      29 </span>            :  * gradients with respect to their input Blob%s, given the error gradients with
<span class="lineNum">      30 </span>            :  * their output Blob%s.
<span class="lineNum">      31 </span>            :  */
<span class="lineNum">      32 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      33 </span>            : class Layer {
<span class="lineNum">      34 </span>            :  public:
<span class="lineNum">      35 </span>            :   /**
<span class="lineNum">      36 </span>            :    * You should not implement your own constructor. Any set up code should go
<span class="lineNum">      37 </span>            :    * to SetUp(), where the dimensions of the bottom blobs are provided to the
<a name="38"><span class="lineNum">      38 </span>            :    * layer.</a>
<span class="lineNum">      39 </span>            :    */
<span class="lineNum">      40 </span><span class="lineCov">         13 :   explicit Layer(const LayerParameter&amp; param)</span>
<span class="lineNum">      41 </span><span class="lineCov">         13 :     : layer_param_(param) {</span>
<span class="lineNum">      42 </span>            :       // Set phase and copy blobs (if there are any).
<span class="lineNum">      43 </span><span class="lineCov">         13 :       phase_ = param.phase();</span>
<span class="lineNum">      44 </span><span class="lineCov">         13 :       if (layer_param_.blobs_size() &gt; 0) {</span>
<span class="lineNum">      45 </span><span class="lineNoCov">          0 :         blobs_.resize(layer_param_.blobs_size());</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :         for (int i = 0; i &lt; layer_param_.blobs_size(); ++i) {</span>
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :           blobs_[i].reset(new Blob&lt;Dtype&gt;());</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :           blobs_[i]-&gt;FromProto(layer_param_.blobs(i));</span>
<span class="lineNum">      49 </span>            :         }
<a name="50"><span class="lineNum">      50 </span>            :       }</a>
<span class="lineNum">      51 </span><span class="lineCov">         13 :     }</span>
<span class="lineNum">      52 </span><span class="lineCov">         26 :   virtual ~Layer() {}</span>
<span class="lineNum">      53 </span>            : 
<span class="lineNum">      54 </span>            :   /**
<span class="lineNum">      55 </span>            :    * @brief Implements common layer setup functionality.
<span class="lineNum">      56 </span>            :    *
<span class="lineNum">      57 </span>            :    * @param bottom the preshaped input blobs
<span class="lineNum">      58 </span>            :    * @param top
<span class="lineNum">      59 </span>            :    *     the allocated but unshaped output blobs, to be shaped by Reshape
<span class="lineNum">      60 </span>            :    *
<span class="lineNum">      61 </span>            :    * Checks that the number of bottom and top blobs is correct.
<span class="lineNum">      62 </span>            :    * Calls LayerSetUp to do special layer setup for individual layer types,
<span class="lineNum">      63 </span>            :    * followed by Reshape to set up sizes of top blobs and internal buffers.
<span class="lineNum">      64 </span>            :    * Sets up the loss weight multiplier blobs for any non-zero loss weights.
<a name="65"><span class="lineNum">      65 </span>            :    * This method may not be overridden.</a>
<span class="lineNum">      66 </span>            :    */
<span class="lineNum">      67 </span><span class="lineCov">         13 :   void SetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      68 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      69 </span><span class="lineCov">         13 :     CheckBlobCounts(bottom, top);</span>
<span class="lineNum">      70 </span><span class="lineCov">         13 :     LayerSetUp(bottom, top);</span>
<span class="lineNum">      71 </span><span class="lineCov">         13 :     Reshape(bottom, top);</span>
<span class="lineNum">      72 </span><span class="lineCov">         13 :     SetLossWeights(top);</span>
<span class="lineNum">      73 </span><span class="lineCov">         13 :   }</span>
<span class="lineNum">      74 </span>            : 
<span class="lineNum">      75 </span>            :   /**
<span class="lineNum">      76 </span>            :    * @brief Does layer-specific setup: your layer should implement this function
<span class="lineNum">      77 </span>            :    *        as well as Reshape.
<span class="lineNum">      78 </span>            :    *
<span class="lineNum">      79 </span>            :    * @param bottom
<span class="lineNum">      80 </span>            :    *     the preshaped input blobs, whose data fields store the input data for
<span class="lineNum">      81 </span>            :    *     this layer
<span class="lineNum">      82 </span>            :    * @param top
<span class="lineNum">      83 </span>            :    *     the allocated but unshaped output blobs
<span class="lineNum">      84 </span>            :    *
<span class="lineNum">      85 </span>            :    * This method should do one-time layer specific setup. This includes reading
<span class="lineNum">      86 </span>            :    * and processing relevent parameters from the &lt;code&gt;layer_param_&lt;/code&gt;.
<span class="lineNum">      87 </span>            :    * Setting up the shapes of top blobs and internal buffers should be done in
<span class="lineNum">      88 </span>            :    * &lt;code&gt;Reshape&lt;/code&gt;, which will be called before the forward pass to
<a name="89"><span class="lineNum">      89 </span>            :    * adjust the top blob sizes.</a>
<span class="lineNum">      90 </span>            :    */
<span class="lineNum">      91 </span><span class="lineCov">          4 :   virtual void LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      92 </span><span class="lineCov">          4 :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {}</span>
<span class="lineNum">      93 </span>            : 
<span class="lineNum">      94 </span>            :   /**
<span class="lineNum">      95 </span>            :    * @brief Adjust the shapes of top blobs and internal buffers to accommodate
<span class="lineNum">      96 </span>            :    *        the shapes of the bottom blobs.
<span class="lineNum">      97 </span>            :    *
<span class="lineNum">      98 </span>            :    * @param bottom the input blobs, with the requested input shapes
<span class="lineNum">      99 </span>            :    * @param top the top blobs, which should be reshaped as needed
<span class="lineNum">     100 </span>            :    *
<span class="lineNum">     101 </span>            :    * This method should reshape top blobs as needed according to the shapes
<span class="lineNum">     102 </span>            :    * of the bottom (input) blobs, as well as reshaping any internal buffers
<span class="lineNum">     103 </span>            :    * and making any other necessary adjustments so that the layer can
<span class="lineNum">     104 </span>            :    * accommodate the bottom blobs.
<span class="lineNum">     105 </span>            :    */
<span class="lineNum">     106 </span>            :   virtual void Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,
<span class="lineNum">     107 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) = 0;
<span class="lineNum">     108 </span>            : 
<span class="lineNum">     109 </span>            :   /**
<span class="lineNum">     110 </span>            :    * @brief Given the bottom blobs, compute the top blobs and the loss.
<span class="lineNum">     111 </span>            :    *
<span class="lineNum">     112 </span>            :    * @param bottom
<span class="lineNum">     113 </span>            :    *     the input blobs, whose data fields store the input data for this layer
<span class="lineNum">     114 </span>            :    * @param top
<span class="lineNum">     115 </span>            :    *     the preshaped output blobs, whose data fields will store this layers'
<span class="lineNum">     116 </span>            :    *     outputs
<span class="lineNum">     117 </span>            :    * \return The total loss from the layer.
<span class="lineNum">     118 </span>            :    *
<span class="lineNum">     119 </span>            :    * The Forward wrapper calls the relevant device wrapper function
<span class="lineNum">     120 </span>            :    * (Forward_cpu or Forward_gpu) to compute the top blob values given the
<span class="lineNum">     121 </span>            :    * bottom blobs.  If the layer has any non-zero loss_weights, the wrapper
<span class="lineNum">     122 </span>            :    * then computes and returns the loss.
<span class="lineNum">     123 </span>            :    *
<span class="lineNum">     124 </span>            :    * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
<span class="lineNum">     125 </span>            :    */
<span class="lineNum">     126 </span>            :   inline Dtype Forward(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,
<span class="lineNum">     127 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top);
<span class="lineNum">     128 </span>            : 
<span class="lineNum">     129 </span>            :   /**
<span class="lineNum">     130 </span>            :    * @brief Given the top blob error gradients, compute the bottom blob error
<span class="lineNum">     131 </span>            :    *        gradients.
<span class="lineNum">     132 </span>            :    *
<span class="lineNum">     133 </span>            :    * @param top
<span class="lineNum">     134 </span>            :    *     the output blobs, whose diff fields store the gradient of the error
<span class="lineNum">     135 </span>            :    *     with respect to themselves
<span class="lineNum">     136 </span>            :    * @param propagate_down
<span class="lineNum">     137 </span>            :    *     a vector with equal length to bottom, with each index indicating
<span class="lineNum">     138 </span>            :    *     whether to propagate the error gradients down to the bottom blob at
<span class="lineNum">     139 </span>            :    *     the corresponding index
<span class="lineNum">     140 </span>            :    * @param bottom
<span class="lineNum">     141 </span>            :    *     the input blobs, whose diff fields will store the gradient of the error
<span class="lineNum">     142 </span>            :    *     with respect to themselves after Backward is run
<span class="lineNum">     143 </span>            :    *
<span class="lineNum">     144 </span>            :    * The Backward wrapper calls the relevant device wrapper function
<span class="lineNum">     145 </span>            :    * (Backward_cpu or Backward_gpu) to compute the bottom blob diffs given the
<span class="lineNum">     146 </span>            :    * top blob diffs.
<span class="lineNum">     147 </span>            :    *
<span class="lineNum">     148 </span>            :    * Your layer should implement Backward_cpu and (optionally) Backward_gpu.
<span class="lineNum">     149 </span>            :    */
<span class="lineNum">     150 </span>            :   inline void Backward(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,
<span class="lineNum">     151 </span>            :       const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">     152 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom);
<span class="lineNum">     153 </span>            : 
<span class="lineNum">     154 </span>            :   /**
<a name="155"><span class="lineNum">     155 </span>            :    * @brief Returns the vector of learnable parameter blobs.</a>
<span class="lineNum">     156 </span>            :    */
<span class="lineNum">     157 </span><span class="lineNoCov">          0 :   vector&lt;shared_ptr&lt;Blob&lt;Dtype&gt; &gt; &gt;&amp; blobs() {</span>
<span class="lineNum">     158 </span><span class="lineNoCov">          0 :     return blobs_;</span>
<span class="lineNum">     159 </span>            :   }
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span>            :   /**
<a name="162"><span class="lineNum">     162 </span>            :    * @brief Returns the layer parameter.</a>
<span class="lineNum">     163 </span>            :    */
<span class="lineNum">     164 </span><span class="lineNoCov">          0 :   const LayerParameter&amp; layer_param() const { return layer_param_; }</span>
<span class="lineNum">     165 </span>            : 
<span class="lineNum">     166 </span>            :   /**
<span class="lineNum">     167 </span>            :    * @brief Writes the layer parameter to a protocol buffer
<span class="lineNum">     168 </span>            :    */
<span class="lineNum">     169 </span>            :   virtual void ToProto(LayerParameter* param, bool write_diff = false);
<span class="lineNum">     170 </span>            : 
<span class="lineNum">     171 </span>            :   /**
<a name="172"><span class="lineNum">     172 </span>            :    * @brief Returns the scalar loss associated with a top blob at a given index.</a>
<span class="lineNum">     173 </span>            :    */
<span class="lineNum">     174 </span><span class="lineNoCov">          0 :   inline Dtype loss(const int top_index) const {</span>
<span class="lineNum">     175 </span><span class="lineCov">       1850 :     return (loss_.size() &gt; top_index) ? loss_[top_index] : Dtype(0);</span>
<span class="lineNum">     176 </span>            :   }
<span class="lineNum">     177 </span>            : 
<span class="lineNum">     178 </span>            :   /**
<a name="179"><span class="lineNum">     179 </span>            :    * @brief Sets the loss associated with a top blob at a given index.</a>
<span class="lineNum">     180 </span>            :    */
<span class="lineNum">     181 </span><span class="lineCov">          2 :   inline void set_loss(const int top_index, const Dtype value) {</span>
<span class="lineNum">     182 </span><span class="lineCov">          2 :     if (loss_.size() &lt;= top_index) {</span>
<span class="lineNum">     183 </span><span class="lineCov">          2 :       loss_.resize(top_index + 1, Dtype(0));</span>
<span class="lineNum">     184 </span>            :     }
<span class="lineNum">     185 </span><span class="lineCov">          2 :     loss_[top_index] = value;</span>
<span class="lineNum">     186 </span><span class="lineCov">          2 :   }</span>
<span class="lineNum">     187 </span>            : 
<span class="lineNum">     188 </span>            :   /**
<a name="189"><span class="lineNum">     189 </span>            :    * @brief Returns the layer type.</a>
<span class="lineNum">     190 </span>            :    */
<span class="lineNum">     191 </span><span class="lineNoCov">          0 :   virtual inline const char* type() const { return &quot;&quot;; }</span>
<span class="lineNum">     192 </span>            : 
<span class="lineNum">     193 </span>            :   /**
<span class="lineNum">     194 </span>            :    * @brief Returns the exact number of bottom blobs required by the layer,
<span class="lineNum">     195 </span>            :    *        or -1 if no exact number is required.
<span class="lineNum">     196 </span>            :    *
<span class="lineNum">     197 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="198"><span class="lineNum">     198 </span>            :    * layer expects some exact number of bottom blobs.</a>
<span class="lineNum">     199 </span>            :    */
<span class="lineNum">     200 </span><span class="lineCov">          2 :   virtual inline int ExactNumBottomBlobs() const { return -1; }</span>
<span class="lineNum">     201 </span>            :   /**
<span class="lineNum">     202 </span>            :    * @brief Returns the minimum number of bottom blobs required by the layer,
<span class="lineNum">     203 </span>            :    *        or -1 if no minimum number is required.
<span class="lineNum">     204 </span>            :    *
<span class="lineNum">     205 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="206"><span class="lineNum">     206 </span>            :    * layer expects some minimum number of bottom blobs.</a>
<span class="lineNum">     207 </span>            :    */
<span class="lineNum">     208 </span><span class="lineCov">         11 :   virtual inline int MinBottomBlobs() const { return -1; }</span>
<span class="lineNum">     209 </span>            :   /**
<span class="lineNum">     210 </span>            :    * @brief Returns the maximum number of bottom blobs required by the layer,
<span class="lineNum">     211 </span>            :    *        or -1 if no maximum number is required.
<span class="lineNum">     212 </span>            :    *
<span class="lineNum">     213 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="214"><span class="lineNum">     214 </span>            :    * layer expects some maximum number of bottom blobs.</a>
<span class="lineNum">     215 </span>            :    */
<span class="lineNum">     216 </span><span class="lineCov">         13 :   virtual inline int MaxBottomBlobs() const { return -1; }</span>
<span class="lineNum">     217 </span>            :   /**
<span class="lineNum">     218 </span>            :    * @brief Returns the exact number of top blobs required by the layer,
<span class="lineNum">     219 </span>            :    *        or -1 if no exact number is required.
<span class="lineNum">     220 </span>            :    *
<span class="lineNum">     221 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="222"><span class="lineNum">     222 </span>            :    * layer expects some exact number of top blobs.</a>
<span class="lineNum">     223 </span>            :    */
<span class="lineNum">     224 </span><span class="lineCov">          8 :   virtual inline int ExactNumTopBlobs() const { return -1; }</span>
<span class="lineNum">     225 </span>            :   /**
<span class="lineNum">     226 </span>            :    * @brief Returns the minimum number of top blobs required by the layer,
<span class="lineNum">     227 </span>            :    *        or -1 if no minimum number is required.
<span class="lineNum">     228 </span>            :    *
<span class="lineNum">     229 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="230"><span class="lineNum">     230 </span>            :    * layer expects some minimum number of top blobs.</a>
<span class="lineNum">     231 </span>            :    */
<span class="lineNum">     232 </span><span class="lineCov">          4 :   virtual inline int MinTopBlobs() const { return -1; }</span>
<span class="lineNum">     233 </span>            :   /**
<span class="lineNum">     234 </span>            :    * @brief Returns the maximum number of top blobs required by the layer,
<span class="lineNum">     235 </span>            :    *        or -1 if no maximum number is required.
<span class="lineNum">     236 </span>            :    *
<span class="lineNum">     237 </span>            :    * This method should be overridden to return a non-negative value if your
<a name="238"><span class="lineNum">     238 </span>            :    * layer expects some maximum number of top blobs.</a>
<span class="lineNum">     239 </span>            :    */
<span class="lineNum">     240 </span><span class="lineCov">          8 :   virtual inline int MaxTopBlobs() const { return -1; }</span>
<span class="lineNum">     241 </span>            :   /**
<span class="lineNum">     242 </span>            :    * @brief Returns true if the layer requires an equal number of bottom and
<span class="lineNum">     243 </span>            :    *        top blobs.
<span class="lineNum">     244 </span>            :    *
<span class="lineNum">     245 </span>            :    * This method should be overridden to return true if your layer expects an
<a name="246"><span class="lineNum">     246 </span>            :    * equal number of bottom and top blobs.</a>
<span class="lineNum">     247 </span>            :    */
<span class="lineNum">     248 </span><span class="lineCov">         11 :   virtual inline bool EqualNumBottomTopBlobs() const { return false; }</span>
<span class="lineNum">     249 </span>            : 
<span class="lineNum">     250 </span>            :   /**
<span class="lineNum">     251 </span>            :    * @brief Return whether &quot;anonymous&quot; top blobs are created automatically
<span class="lineNum">     252 </span>            :    *        by the layer.
<span class="lineNum">     253 </span>            :    *
<span class="lineNum">     254 </span>            :    * If this method returns true, Net::Init will create enough &quot;anonymous&quot; top
<span class="lineNum">     255 </span>            :    * blobs to fulfill the requirement specified by ExactNumTopBlobs() or
<a name="256"><span class="lineNum">     256 </span>            :    * MinTopBlobs().</a>
<span class="lineNum">     257 </span>            :    */
<span class="lineNum">     258 </span><span class="lineCov">         11 :   virtual inline bool AutoTopBlobs() const { return false; }</span>
<span class="lineNum">     259 </span>            : 
<span class="lineNum">     260 </span>            :   /**
<span class="lineNum">     261 </span>            :    * @brief Return whether to allow force_backward for a given bottom blob
<span class="lineNum">     262 </span>            :    *        index.
<span class="lineNum">     263 </span>            :    *
<span class="lineNum">     264 </span>            :    * If AllowForceBackward(i) == false, we will ignore the force_backward
<span class="lineNum">     265 </span>            :    * setting and backpropagate to blob i only if it needs gradient information
<a name="266"><span class="lineNum">     266 </span>            :    * (as is done when force_backward == false).</a>
<span class="lineNum">     267 </span>            :    */
<span class="lineNum">     268 </span><span class="lineNoCov">          0 :   virtual inline bool AllowForceBackward(const int bottom_index) const {</span>
<span class="lineNum">     269 </span><span class="lineNoCov">          0 :     return true;</span>
<span class="lineNum">     270 </span>            :   }
<span class="lineNum">     271 </span>            : 
<span class="lineNum">     272 </span>            :   /**
<span class="lineNum">     273 </span>            :    * @brief Specifies whether the layer should compute gradients w.r.t. a
<span class="lineNum">     274 </span>            :    *        parameter at a particular index given by param_id.
<span class="lineNum">     275 </span>            :    *
<span class="lineNum">     276 </span>            :    * You can safely ignore false values and always compute gradients
<a name="277"><span class="lineNum">     277 </span>            :    * for all parameters, but possibly with wasteful computation.</a>
<span class="lineNum">     278 </span>            :    */
<span class="lineNum">     279 </span><span class="lineNoCov">          0 :   inline bool param_propagate_down(const int param_id) {</span>
<span class="lineNum">     280 </span>            :     return (param_propagate_down_.size() &gt; param_id) ?
<span class="lineNum">     281 </span><span class="lineNoCov">          0 :         param_propagate_down_[param_id] : false;</span>
<span class="lineNum">     282 </span>            :   }
<span class="lineNum">     283 </span>            :   /**
<span class="lineNum">     284 </span>            :    * @brief Sets whether the layer should compute gradients w.r.t. a
<a name="285"><span class="lineNum">     285 </span>            :    *        parameter at a particular index given by param_id.</a>
<span class="lineNum">     286 </span>            :    */
<span class="lineNum">     287 </span><span class="lineCov">          8 :   inline void set_param_propagate_down(const int param_id, const bool value) {</span>
<span class="lineNum">     288 </span><span class="lineCov">          8 :     if (param_propagate_down_.size() &lt;= param_id) {</span>
<span class="lineNum">     289 </span><span class="lineNoCov">          0 :       param_propagate_down_.resize(param_id + 1, true);</span>
<span class="lineNum">     290 </span>            :     }
<span class="lineNum">     291 </span>            :     param_propagate_down_[param_id] = value;
<span class="lineNum">     292 </span><span class="lineCov">          8 :   }</span>
<span class="lineNum">     293 </span>            : 
<span class="lineNum">     294 </span>            : 
<span class="lineNum">     295 </span>            :  protected:
<span class="lineNum">     296 </span>            :   /** The protobuf that stores the layer parameters */
<span class="lineNum">     297 </span>            :   LayerParameter layer_param_;
<span class="lineNum">     298 </span>            :   /** The phase: TRAIN or TEST */
<span class="lineNum">     299 </span>            :   Phase phase_;
<span class="lineNum">     300 </span>            :   /** The vector that stores the learnable parameters as a set of blobs. */
<span class="lineNum">     301 </span>            :   vector&lt;shared_ptr&lt;Blob&lt;Dtype&gt; &gt; &gt; blobs_;
<span class="lineNum">     302 </span>            :   /** Vector indicating whether to compute the diff of each param blob. */
<span class="lineNum">     303 </span>            :   vector&lt;bool&gt; param_propagate_down_;
<span class="lineNum">     304 </span>            : 
<span class="lineNum">     305 </span>            :   /** The vector that indicates whether each top blob has a non-zero weight in
<span class="lineNum">     306 </span>            :    *  the objective function. */
<span class="lineNum">     307 </span>            :   vector&lt;Dtype&gt; loss_;
<span class="lineNum">     308 </span>            : 
<span class="lineNum">     309 </span>            :   /** @brief Using the CPU device, compute the layer output. */
<span class="lineNum">     310 </span>            :   virtual void Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,
<span class="lineNum">     311 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) = 0;
<span class="lineNum">     312 </span>            :   /**
<span class="lineNum">     313 </span>            :    * @brief Using the GPU device, compute the layer output.
<a name="314"><span class="lineNum">     314 </span>            :    *        Fall back to Forward_cpu() if unavailable.</a>
<span class="lineNum">     315 </span>            :    */
<span class="lineNum">     316 </span><span class="lineNoCov">          0 :   virtual void Forward_gpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">     317 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">     318 </span>            :     // LOG(WARNING) &lt;&lt; &quot;Using CPU code as backup.&quot;;
<span class="lineNum">     319 </span><span class="lineNoCov">          0 :     return Forward_cpu(bottom, top);</span>
<span class="lineNum">     320 </span>            :   }
<span class="lineNum">     321 </span>            : 
<span class="lineNum">     322 </span>            :   /**
<span class="lineNum">     323 </span>            :    * @brief Using the CPU device, compute the gradients for any parameters and
<span class="lineNum">     324 </span>            :    *        for the bottom blobs if propagate_down is true.
<span class="lineNum">     325 </span>            :    */
<span class="lineNum">     326 </span>            :   virtual void Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,
<span class="lineNum">     327 </span>            :       const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">     328 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) = 0;
<span class="lineNum">     329 </span>            :   /**
<span class="lineNum">     330 </span>            :    * @brief Using the GPU device, compute the gradients for any parameters and
<span class="lineNum">     331 </span>            :    *        for the bottom blobs if propagate_down is true.
<a name="332"><span class="lineNum">     332 </span>            :    *        Fall back to Backward_cpu() if unavailable.</a>
<span class="lineNum">     333 </span>            :    */
<span class="lineNum">     334 </span><span class="lineNoCov">          0 :   virtual void Backward_gpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">     335 </span>            :       const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">     336 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     337 </span>            :     // LOG(WARNING) &lt;&lt; &quot;Using CPU code as backup.&quot;;
<span class="lineNum">     338 </span><span class="lineNoCov">          0 :     Backward_cpu(top, propagate_down, bottom);</span>
<span class="lineNum">     339 </span><span class="lineNoCov">          0 :   }</span>
<span class="lineNum">     340 </span>            : 
<span class="lineNum">     341 </span>            :   /**
<span class="lineNum">     342 </span>            :    * Called by the parent Layer's SetUp to check that the number of bottom
<span class="lineNum">     343 </span>            :    * and top Blobs provided as input match the expected numbers specified by
<a name="344"><span class="lineNum">     344 </span>            :    * the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.</a>
<span class="lineNum">     345 </span>            :    */
<span class="lineNum">     346 </span><span class="lineCov">         13 :   virtual void CheckBlobCounts(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">     347 </span>            :                                const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">     348 </span><span class="lineCov">         13 :     if (ExactNumBottomBlobs() &gt;= 0) {</span>
<span class="lineNum">     349 </span><span class="lineCov">         33 :       CHECK_EQ(ExactNumBottomBlobs(), bottom.size())</span>
<span class="lineNum">     350 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer takes &quot; &lt;&lt; ExactNumBottomBlobs()</span>
<span class="lineNum">     351 </span>            :           &lt;&lt; &quot; bottom blob(s) as input.&quot;;
<span class="lineNum">     352 </span>            :     }
<span class="lineNum">     353 </span><span class="lineCov">         13 :     if (MinBottomBlobs() &gt;= 0) {</span>
<span class="lineNum">     354 </span><span class="lineCov">          6 :       CHECK_LE(MinBottomBlobs(), bottom.size())</span>
<span class="lineNum">     355 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer takes at least &quot; &lt;&lt; MinBottomBlobs()</span>
<span class="lineNum">     356 </span>            :           &lt;&lt; &quot; bottom blob(s) as input.&quot;;
<span class="lineNum">     357 </span>            :     }
<span class="lineNum">     358 </span><span class="lineCov">         13 :     if (MaxBottomBlobs() &gt;= 0) {</span>
<span class="lineNum">     359 </span><span class="lineNoCov">          0 :       CHECK_GE(MaxBottomBlobs(), bottom.size())</span>
<span class="lineNum">     360 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer takes at most &quot; &lt;&lt; MaxBottomBlobs()</span>
<span class="lineNum">     361 </span>            :           &lt;&lt; &quot; bottom blob(s) as input.&quot;;
<span class="lineNum">     362 </span>            :     }
<span class="lineNum">     363 </span><span class="lineCov">         13 :     if (ExactNumTopBlobs() &gt;= 0) {</span>
<span class="lineNum">     364 </span><span class="lineCov">         12 :       CHECK_EQ(ExactNumTopBlobs(), top.size())</span>
<span class="lineNum">     365 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer produces &quot; &lt;&lt; ExactNumTopBlobs()</span>
<span class="lineNum">     366 </span>            :           &lt;&lt; &quot; top blob(s) as output.&quot;;
<span class="lineNum">     367 </span>            :     }
<span class="lineNum">     368 </span><span class="lineCov">         13 :     if (MinTopBlobs() &gt;= 0) {</span>
<span class="lineNum">     369 </span><span class="lineCov">         27 :       CHECK_LE(MinTopBlobs(), top.size())</span>
<span class="lineNum">     370 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer produces at least &quot; &lt;&lt; MinTopBlobs()</span>
<span class="lineNum">     371 </span>            :           &lt;&lt; &quot; top blob(s) as output.&quot;;
<span class="lineNum">     372 </span>            :     }
<span class="lineNum">     373 </span><span class="lineCov">         13 :     if (MaxTopBlobs() &gt;= 0) {</span>
<span class="lineNum">     374 </span><span class="lineCov">         15 :       CHECK_GE(MaxTopBlobs(), top.size())</span>
<span class="lineNum">     375 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer produces at most &quot; &lt;&lt; MaxTopBlobs()</span>
<span class="lineNum">     376 </span>            :           &lt;&lt; &quot; top blob(s) as output.&quot;;
<span class="lineNum">     377 </span>            :     }
<span class="lineNum">     378 </span><span class="lineCov">         13 :     if (EqualNumBottomTopBlobs()) {</span>
<span class="lineNum">     379 </span><span class="lineCov">          8 :       CHECK_EQ(bottom.size(), top.size())</span>
<span class="lineNum">     380 </span><span class="lineNoCov">          0 :           &lt;&lt; type() &lt;&lt; &quot; Layer produces one top blob as output for each &quot;</span>
<span class="lineNum">     381 </span>            :           &lt;&lt; &quot;bottom blob input.&quot;;
<span class="lineNum">     382 </span>            :     }
<span class="lineNum">     383 </span><span class="lineCov">         13 :   }</span>
<span class="lineNum">     384 </span>            : 
<span class="lineNum">     385 </span>            :   /**
<span class="lineNum">     386 </span>            :    * Called by SetUp to initialize the weights associated with any top blobs in
<a name="387"><span class="lineNum">     387 </span>            :    * the loss function. Store non-zero loss weights in the diff blob.</a>
<span class="lineNum">     388 </span>            :    */
<span class="lineNum">     389 </span><span class="lineCov">         13 :   inline void SetLossWeights(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {</span>
<span class="lineNum">     390 </span>            :     const int num_loss_weights = layer_param_.loss_weight_size();
<span class="lineNum">     391 </span><span class="lineCov">         13 :     if (num_loss_weights) {</span>
<span class="lineNum">     392 </span><span class="lineCov">          8 :       CHECK_EQ(top.size(), num_loss_weights) &lt;&lt; &quot;loss_weight must be &quot;</span>
<span class="lineNum">     393 </span>            :           &quot;unspecified or specified once per top blob.&quot;;
<span class="lineNum">     394 </span><span class="lineCov">         10 :       for (int top_id = 0; top_id &lt; top.size(); ++top_id) {</span>
<span class="lineNum">     395 </span><span class="lineNoCov">          0 :         const Dtype loss_weight = layer_param_.loss_weight(top_id);</span>
<span class="lineNum">     396 </span><span class="lineCov">          2 :         if (loss_weight == Dtype(0)) { continue; }</span>
<span class="lineNum">     397 </span><span class="lineCov">          2 :         this-&gt;set_loss(top_id, loss_weight);</span>
<span class="lineNum">     398 </span><span class="lineCov">          2 :         const int count = top[top_id]-&gt;count();</span>
<span class="lineNum">     399 </span><span class="lineCov">          2 :         Dtype* loss_multiplier = top[top_id]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     400 </span><span class="lineCov">          2 :         caffe_set(count, loss_weight, loss_multiplier);</span>
<span class="lineNum">     401 </span>            :       }
<span class="lineNum">     402 </span>            :     }
<span class="lineNum">     403 </span><span class="lineCov">         13 :   }</span>
<span class="lineNum">     404 </span>            : 
<span class="lineNum">     405 </span>            :  private:
<span class="lineNum">     406 </span>            :   DISABLE_COPY_AND_ASSIGN(Layer);
<span class="lineNum">     407 </span>            : };  // class Layer
<span class="lineNum">     408 </span>            : 
<span class="lineNum">     409 </span>            : // Forward and backward wrappers. You should implement the cpu and
<span class="lineNum">     410 </span>            : // gpu specific implementations instead, and should not change these
<a name="411"><span class="lineNum">     411 </span>            : // functions.</a>
<span class="lineNum">     412 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     413 </span><span class="lineCov">       1300 : inline Dtype Layer&lt;Dtype&gt;::Forward(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">     414 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">     415 </span>            :   Dtype loss = 0;
<span class="lineNum">     416 </span><span class="lineCov">       1300 :   Reshape(bottom, top);</span>
<span class="lineNum">     417 </span><span class="lineCov">       1300 :   switch (Caffe::mode()) {</span>
<span class="lineNum">     418 </span>            :   case Caffe::CPU:
<span class="lineNum">     419 </span><span class="lineCov">       1300 :     Forward_cpu(bottom, top);</span>
<span class="lineNum">     420 </span><span class="lineCov">       7400 :     for (int top_id = 0; top_id &lt; top.size(); ++top_id) {</span>
<span class="lineNum">     421 </span><span class="lineCov">       1600 :       if (!this-&gt;loss(top_id)) { continue; }</span>
<span class="lineNum">     422 </span><span class="lineCov">        200 :       const int count = top[top_id]-&gt;count();</span>
<span class="lineNum">     423 </span><span class="lineCov">        200 :       const Dtype* data = top[top_id]-&gt;cpu_data();</span>
<span class="lineNum">     424 </span><span class="lineCov">        200 :       const Dtype* loss_weights = top[top_id]-&gt;cpu_diff();</span>
<span class="lineNum">     425 </span><span class="lineCov">        200 :       loss += caffe_cpu_dot(count, data, loss_weights);</span>
<span class="lineNum">     426 </span>            :     }
<span class="lineNum">     427 </span>            :     break;
<span class="lineNum">     428 </span>            :   case Caffe::GPU:
<span class="lineNum">     429 </span><span class="lineNoCov">          0 :     Forward_gpu(bottom, top);</span>
<span class="lineNum">     430 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     431 </span>            :     for (int top_id = 0; top_id &lt; top.size(); ++top_id) {
<span class="lineNum">     432 </span>            :       if (!this-&gt;loss(top_id)) { continue; }
<span class="lineNum">     433 </span>            :       const int count = top[top_id]-&gt;count();
<span class="lineNum">     434 </span>            :       const Dtype* data = top[top_id]-&gt;gpu_data();
<span class="lineNum">     435 </span>            :       const Dtype* loss_weights = top[top_id]-&gt;gpu_diff();
<span class="lineNum">     436 </span>            :       Dtype blob_loss = 0;
<span class="lineNum">     437 </span>            :       caffe_gpu_dot(count, data, loss_weights, &amp;blob_loss);
<span class="lineNum">     438 </span>            :       loss += blob_loss;
<span class="lineNum">     439 </span>            :     }
<span class="lineNum">     440 </span>            : #endif
<span class="lineNum">     441 </span><span class="lineNoCov">          0 :     break;</span>
<span class="lineNum">     442 </span>            :   default:
<span class="lineNum">     443 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown caffe mode.&quot;;</span>
<span class="lineNum">     444 </span>            :   }
<span class="lineNum">     445 </span><span class="lineCov">       1300 :   return loss;</span>
<span class="lineNum">     446 </span>            : }
<a name="447"><span class="lineNum">     447 </span>            : </a>
<span class="lineNum">     448 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     449 </span><span class="lineNoCov">          0 : inline void Layer&lt;Dtype&gt;::Backward(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">     450 </span>            :     const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">     451 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     452 </span><span class="lineNoCov">          0 :   switch (Caffe::mode()) {</span>
<span class="lineNum">     453 </span>            :   case Caffe::CPU:
<span class="lineNum">     454 </span><span class="lineNoCov">          0 :     Backward_cpu(top, propagate_down, bottom);</span>
<span class="lineNum">     455 </span><span class="lineNoCov">          0 :     break;</span>
<span class="lineNum">     456 </span>            :   case Caffe::GPU:
<span class="lineNum">     457 </span><span class="lineNoCov">          0 :     Backward_gpu(top, propagate_down, bottom);</span>
<span class="lineNum">     458 </span><span class="lineNoCov">          0 :     break;</span>
<span class="lineNum">     459 </span>            :   default:
<span class="lineNum">     460 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown caffe mode.&quot;;</span>
<span class="lineNum">     461 </span>            :   }
<span class="lineNum">     462 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     463 </span>            : 
<a name="464"><span class="lineNum">     464 </span>            : // Serialize LayerParameter to protocol buffer</a>
<span class="lineNum">     465 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     466 </span><span class="lineNoCov">          0 : void Layer&lt;Dtype&gt;::ToProto(LayerParameter* param, bool write_diff) {</span>
<span class="lineNum">     467 </span><span class="lineNoCov">          0 :   param-&gt;Clear();</span>
<span class="lineNum">     468 </span><span class="lineNoCov">          0 :   param-&gt;CopyFrom(layer_param_);</span>
<span class="lineNum">     469 </span>            :   param-&gt;clear_blobs();
<span class="lineNum">     470 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; blobs_.size(); ++i) {</span>
<span class="lineNum">     471 </span><span class="lineNoCov">          0 :     blobs_[i]-&gt;ToProto(param-&gt;add_blobs(), write_diff);</span>
<span class="lineNum">     472 </span>            :   }
<span class="lineNum">     473 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     474 </span>            : 
<span class="lineNum">     475 </span>            : }  // namespace caffe
<span class="lineNum">     476 </span>            : 
<span class="lineNum">     477 </span>            : #endif  // CAFFE_LAYER_H_
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
